<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vue学习使用</title>
</head>

<body>
  <div id="app">
    <button @click="add">添加用户</button>
    <hr>
    <ul>
      <!-- 条件渲染 -->
      <!-- template标签 -->
      <template v-if="users.length===0">
        <li>数据加载中...</li>
      </template>
      <template v-else>
        <li v-for="item in users" :key="item.id">
          <span>{{item.name}}</span>
          <span>
            <button @click="edit(item.id)">修改</button>
          </span>
          <span>
            <button @click="del(item.id)">删除</button>
          </span>
        </li>
      </template>

    </ul>
  </div>

  <script src="./js/vue.js"></script>
  <script src="./js/axios.js"></script>
  <script src="./utils/http.js"></script>
  <script>

    const vm = new Vue({
      el: '#app',
      data: {
        // 定义数据源
        users: []
      },
      async mounted() {
        // this.users = await (await get('/api/users')).data
        let users = await get('/api/users')
        this.users = users.data
      },
      methods: {
        async add() {
          let users = await post(`/api/users`, { name: Date.now() + '' })
          this.users = users.data
        },
        async edit(id) {
          let users = await put(`/api/users/${id}`, { name: Date.now() + '' })
          this.users = users.data
        },
        async del(id) {
          let users = await del(`/api/users/${id}`)
          this.users = users.data
        }
      }
    })
  </script>

</body>

</html>